package com.ruoyi.tmsdriveracc.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.tmsdriveracc.domain.*;
import com.ruoyi.tmsdriveracc.mapper.AErrorDao;
import com.ruoyi.tmsdriveracc.mapper.AErrorImagesDao;
import com.ruoyi.tmsdriveracc.mapper.AWaybillDao;
import com.ruoyi.tmsdriveracc.mapper.DriverCheckMapper;
import com.ruoyi.tmsdriveracc.service.AErrorService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * (AError)表服务实现类
 *
 * @author 小何
 * @since 2022-04-24 00:13:02
 */
@Service("aErrorService")
public class AErrorServiceImpl implements AErrorService {
    @Resource
    private AErrorDao aErrorDao;
    @Resource
    private AErrorImagesDao aErrorImagesDao;
    @Resource
    private AWaybillDao aWaybillDao;
    @Autowired(required = false)
    private DriverCheckMapper driverCheckMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public AError queryById(Waybill waybill) {
        AError aError = new AError();
        AError aError1 = aErrorDao.queryById(Integer.valueOf(waybill.getContractId()));

        if (aError1 != null) {
            aError = aError1;
            //处理图片集合
            List<AErrorImages> aErrorImages = new ArrayList<>();
            aErrorImages = aErrorImagesDao.queryAllById(aError.getId());
            List<String> strings = new ArrayList<>();
            for (int i = 0; i < aErrorImages.size(); i++) {
                strings.add(aErrorImages.get(i).getPath());
            }
            aError.setaErrorImages(strings);
        }


        //处理货物信息
        AWaybill aWaybill = aWaybillDao.queryById(waybill.getWaybillId());
        aError.setaWaybill(aWaybill);
        return aError;
    }

    /**
     * 分页查询
     *
     * @param aError      筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<AError> queryByPage(AError aError, PageRequest pageRequest) {
        long total = this.aErrorDao.count(aError);
        return new PageImpl<>(this.aErrorDao.queryAllByLimit(aError, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param aError 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public AError insert(AError aError) {
        //更新状态 已收货 有差错
        driverCheckMapper.updateDriverCheck1(aError);
        //短信通知
        Waybill waybill = new Waybill();
        waybill=driverCheckMapper.selectPhoneByContractId(aError.getWaybillId());

        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("Mobile", waybill.getSenderMobile());
        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("Mobile", waybill.getReceiverMobile());
//        消息通知
//        发给发货人
        rabbitTemplate.convertAndSend("queueSms", JSON.toJSONString(map1));
//        发给收货人
        rabbitTemplate.convertAndSend("queueSms", JSON.toJSONString(map2));
        //增加差错
        this.aErrorDao.insert(aError);
        //增加回执
        Receipt receipt = new Receipt();
        receipt.setContractId(aError.getContractId().toString());
        receipt.setAcceptanceRecords(aError.getAcceptanceRecords());
        driverCheckMapper.insertReceipt(receipt);
        return aError;
    }

    /**
     * 修改数据
     *
     * @param aError 实例对象
     * @return 实例对象
     */
    @Override
    public AError update(AError aError) {
        this.aErrorDao.update(aError);
//        this.queryById(aError.getId());
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.aErrorDao.deleteById(id) > 0;
    }
}
